﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VTS.DAL
{
   public class DanhMucTinTucDAL
    {
       /// <summary>
       /// get all news category
       /// </summary>
       /// <returns></returns>
       public List<NhomTinTuc> GetCategoryAll()
       {
           try
           {
               var context = new VoThiSauDataContext();
               return context.NhomTinTucs.Select(x => x).ToList();
           }
           catch (Exception)
           {
               
               throw;
           }
           
       }

       /// <summary>
       /// get news category by id
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public NhomTinTuc GetCategoryById(int id)
       {
           try
           {
               var context = new VoThiSauDataContext();
               return context.NhomTinTucs.Where(x => x.MaNhomTT == id).Single();
           }
           catch (Exception)
           {
               
               throw;
           }

           
       }

       /// <summary>
       /// create news category
       /// </summary>
       /// <param name="nhomTinTuc"></param>
       /// <returns></returns>
       public int CreateNewsCategory(NhomTinTuc nhomTinTuc)
       {
           try
           {
               var context = new VoThiSauDataContext();

               var ntt = new NhomTinTuc()
                             {
                                 TenNhomTT = nhomTinTuc.TenNhomTT,

                             };
               context.NhomTinTucs.InsertOnSubmit(ntt);
               context.SubmitChanges();
               return ntt.MaNhomTT;
           }
           catch (Exception)
           {
               
               throw;
           }
       }
       /// <summary>
       /// Update category news
       /// </summary>
       /// <param name="id"></param>
       /// <param name="name"></param>
       /// <returns></returns>
       public int UpdateNewsCategory(int id, string name)
       {
           try
           {
               var context = new VoThiSauDataContext();
               NhomTinTuc tinTuc = context.NhomTinTucs.Single(x => x.MaNhomTT == id);
               tinTuc.TenNhomTT = name;
               context.SubmitChanges();
               return tinTuc.MaNhomTT;
           }
           catch (Exception)
           {

               return -1;
           }
       }
       /// <summary>
       /// Delete news category
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public int DeleteNewsCategory(int id)
       {
           try
           {
               var context = new VoThiSauDataContext();
               NhomTinTuc cate = context.NhomTinTucs.Single(x => x.MaNhomTT == id);
               context.NhomTinTucs.DeleteOnSubmit(cate);
               context.SubmitChanges();
               return 0;
           }
           catch (Exception)
           {

               return -1;
           }
       }
    }
}
